FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/python:3.8-alpine

# 设置中文编码和阿里云镜像源
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装系统依赖（移除了重复的python3/pip3）
RUN apk add --no-cache \
    openjdk8-jre \
    wget \
    tar \
    curl \
    openssl-dev \
    libffi-dev \
    gcc \
    musl-dev \
    python3-dev

ENV DATAX_HOME=/opt/datax
WORKDIR $DATAX_HOME

# 先复制requirements.txt以利用缓存
COPY requirements.txt .

# 配置pip并安装Python依赖
RUN pip install --no-cache-dir \
    -r requirements.txt \
    -i https://pypi.tuna.tsinghua.edu.cn/simple \
    --trusted-host pypi.tuna.tsinghua.edu.cn

# 复制并解压DataX
COPY datax.tar.gz .
RUN tar -zxvf datax.tar.gz --strip-components=1 && \
    rm datax.tar.gz && \
    find plugin/ -name "._*" -delete

# 安装Jython
COPY jython-installer-2.7.2.jar .
RUN java -jar jython-installer-2.7.2.jar -s -d /usr/local/jython && \
    ln -s /usr/local/jython/bin/jython /usr/local/bin/jython && \
    sed -i 's|#!/bin/bash|#!/bin/sh|g' /usr/local/jython/bin/jython && \
    rm jython-installer-2.7.2.jar

# 设置工作目录和权限
VOLUME /opt/datax/job
COPY MySQL2MySQL /opt/datax/MySQL2MySQL
COPY run_sync.sh /opt/datax/
RUN chmod +x /opt/datax/bin/datax.py && chmod +x /opt/datax/run_sync.sh

# 健康检查（改为检查DataX）
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
    CMD /opt/datax/bin/datax.py --version || exit 1

ENTRYPOINT ["tail", "-f", "/dev/null"]